package com.ga.controller;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.ga.model.Album;
import com.ga.model.Reply;
import com.ga.model.User;
import com.ga.model.Video;
import com.ga.service.UploadService;
import com.ga.util.Common;
import com.ga.util.MessageUtil;
import com.jfinal.core.ActionKey;
import com.jfinal.core.Controller;
import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.Page;

public class AlbumController extends Controller {
	@ActionKey("/album/list")
	public void list(){
		Map<String,Object> paraMap=new HashMap<String, Object>();
		paraMap.put("a.aname",getPara("aname"));
		paraMap.put("a.description",getPara("description"));
		paraMap.put("a.isenable",1);
		paraMap.put("a.tag",getPara("tag"));
		paraMap.put("u.uname",getPara("uname"));
		paraMap.put("a.createtime1",getPara("createtime1"));
		paraMap.put("a.createtime2",getPara("createtime2"));
		paraMap.put("a.lastmodifytime1",getPara("lastmodifytime1"));
		paraMap.put("a.lastmodifytime2",getPara("lastmodifytime2"));
		Map<String,Object> wheresMap=Common.wheres(paraMap);
		int pn = Integer.parseInt(getPara("pageNumber")==null?"1":getPara("pageNumber"));
		int ps = Integer.parseInt(getPara("pageSize")==null?"10":getPara("pageSize"));	
		Object[] param=(Object[]) wheresMap.get("param");
		String select="select a.id,a.front_pic,a.aname,u.uname,u.nickname,a.createtime,a.lastmodifytime";
		String sqlExceptSelect=" from album a left join users u on(a.users_id=u.id) where  ";
		Page<Album> list=Album.dao.paginate(pn, ps, select, sqlExceptSelect+wheresMap.get("wheres"),param);
		renderJson(list);
	}
	
	@ActionKey("/album/frontpic")
	public void avatar(){
		  Map<String,String> m=new HashMap<String,String>();
		  try{
			int maxFileSize =1024*1024*1; //1M
			int maxMemSize = 1024*1024*1; //1M
			String filePath = this.getRequest().getRealPath("")+"\\resources\\album\\";
			String id=getPara("id");
			String contentType = this.getRequest().getContentType();
			if ((contentType.indexOf("multipart/form-data") >= 0)) {
				String fileName=UploadService.instance.upload(maxMemSize, maxFileSize, id, filePath, this.getRequest());
				m.put("imgsrc", "\\resources\\album\\"+fileName);
			}
		} catch (Exception e) {
			m.put("imgsrc", "");
			e.printStackTrace();
		}
		 renderJson(m);
	}
	
	@ActionKey("/album/add/save")
	public void addsave(){
		Map<String,Object> map=new HashMap<String,Object>();
		try {
			Album album=new Album();
			Album a=album.findFirst("select count(1) c from album where aname=? ",getPara("aname"));
			if(Integer.parseInt(a.get("c").toString())>0){
				map.put("flag", MessageUtil.FAILED.toString());
				map.put("msg","该专辑名称已存在 !");
			}else{
				album.set("aname", getPara("aname"));
				album.set("front_pic", getPara("front_pic"));
				album.set("description", getPara("description"));
				album.set("users_id", getPara("users_id"));
				album.set("lastmodifytime", new Date());
				album.save();
				map.put("flag", MessageUtil.SUCCESS.toString());
			}
		} catch (Exception e) {
			map.put("flag", MessageUtil.FAILED.toString());
			e.printStackTrace();
		}
		renderJson(map);
	}
	@ActionKey("/album/detail")
	public void detail(){
		Map<String,Object> map=new HashMap<String,Object>();
		String id=getPara(0)==null?getPara("id"):getPara(0);
		try {
			String sql="select a.*,u.uname from album a left join users u on(a.users_id=u.id) where a.id=? ";
			Album album=Album.dao.findFirst(sql,id);
			map.put("flag", MessageUtil.SUCCESS.toString());
			map.put("album", album);
		} catch (Exception e) {
			map.put("flag", MessageUtil.FAILED.toString());
			e.printStackTrace();
		}
		renderJson(map);
	}
	@ActionKey("/album/update/save")
	public void updsave(){
		Map<String,Object> map=new HashMap<String,Object>();
		try {
			Album album=new Album();
			album.set("id", getPara("id"));
			album.set("aname", getPara("aname"));
			album.set("front_pic", getPara("front_pic"));
			album.set("description", getPara("description"));
			album.set("tag", getPara("tag"));
			album.set("lastmodifytime", new Date());
			album.update();
			map.put("flag", MessageUtil.SUCCESS.toString());
		} catch (Exception e) {
			map.put("flag", MessageUtil.FAILED.toString());
			e.printStackTrace();
		}
		renderJson(map);
	}
	@ActionKey("/album/delete")
	public void delete(){
		Map<String,Object> map=new HashMap<String,Object>();
		String id=getPara(0)==null?getPara("id"):getPara(0);
		try {
			Db.update("update album set isenable=0 where id=?",id);
			Db.update("update video set isenable=0 where album_id=?",id);
			List<Video> videos=Video.dao.find("select id from video where album_id=?",id);
			for (int i = 0; i < videos.size(); i++) {
				Db.update("update reply set isenable=0 where video_id=?",videos.get(i).get("id"));
				List<Reply> clist=Reply.dao.find("select id from reply where video_id = ? ",videos.get(i).get("id"));
				delchild(clist);
			}
			map.put("flag", MessageUtil.SUCCESS.toString());
		} catch (Exception e) {
			map.put("flag", MessageUtil.FAILED.toString());
			e.printStackTrace();
		}
		renderJson(map);
	}
	
	private void delchild(List<Reply> list){
		for (int i = 0; i < list.size(); i++) {
			String id=list.get(i).get("id").toString();
			List<Reply> clist=Reply.dao.find("select id from reply where parentid = ? ",id);
			if(clist.size()>0){
				Db.update("update reply set isenable = 0 where parentid = ?",id);
				delchild(clist);
			}
		}
	}
}
